package edu.eas.admin.service.impl;

import com.github.pagehelper.PageHelper;
import edu.eas.admin.mapper.DictDataMapper;
import edu.eas.admin.service.AdminService;
import edu.eas.admin.service.DictDataService;
import edu.eas.common.pojo.Admin;
import edu.eas.common.pojo.DictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author cql
 */
@Service
public class DictDataServiceImpl implements DictDataService {

    @Autowired
    private DictDataMapper dictDataMapper;

    @Autowired
    private AdminService adminService;


    @Override
    public List<DictData> list(Integer pageSize, Integer pageNum, String dictLabel, String dictType, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        return dictDataMapper.selectListByParams(dictLabel, dictType, status);
    }

    @Override
    public int create(DictData dict) {
        Admin admin = adminService.getCurrentAdmin();
        Date now = new Date();
        // 直接新增,需要做数据校验么,比如字典标签一样
        dict.setCreateBy(admin.getAdminId());
        dict.setCreateTime(now);
        dict.setUpdateBy(admin.getAdminId());
        dict.setUpdateTime(now);
        return dictDataMapper.insert(dict);
    }

    @Override
    public int delete(Long[] dictCodes) {
        if (dictCodes.length == 0) {
            return 0;
        }
        return dictDataMapper.deleteDictDataByIds(dictCodes);
    }

    @Override
    public int update(DictData dictData) {
        Admin admin = adminService.getCurrentAdmin();
        dictData.setUpdateBy(admin.getAdminId());
        dictData.setUpdateTime(new Date());
        return dictDataMapper.update(dictData);
    }

    @Override
    public DictData getByDictCode(Long dictCode) {
        return dictDataMapper.selectByPrimaryKey(dictCode);
    }
}
